﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BL;
using BO;

namespace AnimalFarm.Field_Building_manage
{
    public partial class AddNewHomeField : Form
    {
        public AddNewHomeField()
        {
            InitializeComponent();
            loadBuilding();
            rdtpDate.Value = DateTime.Now;
        }

        private void loadBuilding()
        {
            B_Building bb = B_Building.CreateInstance();
            if (bb != null)
            {
                DataTable dTable = bb.GetAllBuilding();
                if (dTable != null)
                {
                    ddlbuilding.DataSource = dTable;
                    ddlbuilding.ValueMember = "BuildingID";
                    ddlbuilding.DisplayMember = "BuildingName";
                }
            }
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            string homeFieldName = txtHomeFieldName.Text;
            int buildingID = int.Parse(ddlbuilding.SelectedValue.ToString());
            DateTime dt = rdtpDate.Value;

            B_HomeField bhf = B_HomeField.CreateInstance();
            HomeField_Info hfi = new HomeField_Info();

            if (buildingID > 0 && homeFieldName != null)
            {
                hfi.BuildingID = buildingID;
                hfi.HomeFiledName = homeFieldName;
                hfi.PasteurisingDate = dt;

                if (hfi != null)
                {
                    bool isEffected = bhf.CreateOneHomeField(hfi);
                    if (isEffected == true)
                    {
                        MessageBox.Show("Create a new homefield successfully!", "Creating successuflly", MessageBoxButtons.OK);
                        txtHomeFieldName.Text = "";
                        txtHomeFieldName.Focus();
                    }
                    else
                    {
                        MessageBox.Show("Cannot create a new homefield successfully!", "Missing Data", MessageBoxButtons.OK);
                        txtHomeFieldName.Focus();
                    }
                }
            }
        }
    }
}
